//
//  ViewController.m
//  DownloadImagesAsynchronously
//
//  Created by admin on 2019/8/15.
//  Copyright © 2019 猫爷爷的酱醋茶. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)UIImageView *imageView;

@end

@implementation ViewController

-(void)loadView
{
    //初始化scrollView
    self.scrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.scrollView.backgroundColor = [UIColor whiteColor];
    self.view = self.scrollView;
    
    //初始化imageView
    self.imageView = [[UIImageView alloc]init];
    [self.scrollView addSubview:self.imageView];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(downLoadImage) object:nil];
    [thread start];
}

- (void)downLoadImage
{
    //图片地址
    NSURL *url = [NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1566409677&di=74e2b4870f47befc92e7e5c14feca2bf&imgtype=jpg&er=1&src=http%3A%2F%2Fimg.app178.com%2Ftu%2F201410%2Fxjgpfwrdpqy.jpg"];
    //下载图片
    NSData *data = [NSData dataWithContentsOfURL:url];
    
    //把NSData转为UIImage
    UIImage *image = [UIImage imageWithData:data];
    
    //在主线程上更新UI控件 线程间通信 - [UIImageView setImage:] must be used from main thread only
    //waitUntilDone 值是YES 会等待方法执行完毕 才会执行后续代码
    [self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
    
    
    
    
    
}

-(void)updateUI:(UIImage *)image
{
    self.imageView.image = image;
    
    //让imageView的大小和图片大小一致
    [self.imageView sizeToFit];
    
    //设置UIScrollView的大小
    self.scrollView.contentSize = image.size;
}

@end
